package com.xiaoq.shop.service.impl;

import com.xiaoq.shop.entity.CatalogEntity;
import com.xiaoq.shop.entity.SPUCatalog;
import com.xiaoq.shop.repository.ICatalogRepository;
import com.xiaoq.shop.service.ICatalogService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by chenjh on 2017/11/3.
 */
@Service
public class CatalogServiceImpl implements ICatalogService {

    @Autowired
    private ICatalogRepository catalogRepository;

    /**
     *  门票是一种特殊的商品，此处取catalog为0的门票
     *  0 写死的
     * @return
     */
    public CatalogEntity getTicketCatalogList(){
        return catalogRepository.getTicketCatalogList();
    }

    @Override
    public Map<String, String> getTicketCode2TypeMap() {
        Map<String, String> resultMap = new HashMap<>();
        CatalogEntity catalogEntity = catalogRepository.getTicketCatalogList();
        if(catalogEntity != null){
            List<SPUCatalog> spuCatalogList = catalogEntity.getSpuCatalogs();
            if(CollectionUtils.isNotEmpty(spuCatalogList)){
                List<SPUCatalog.TicketType> typeList = spuCatalogList.get(0).getTicketTypes();
                if(CollectionUtils.isNotEmpty(typeList)){
                    resultMap = typeList.stream().collect(Collectors.toMap(SPUCatalog.TicketType::getCode, SPUCatalog.TicketType::getName));
                }
            }
        }

        return resultMap;
    }
}
